package lzyc.pointsManagement.business.userdept.controller;

import lzyc.pointsManagement.business.config.DeptGroupEnum;
import lzyc.pointsManagement.business.config.RoleEnum;
import lzyc.pointsManagement.business.userdept.domain.SysUserDept;
import lzyc.pointsManagement.business.userdept.service.SysUserDeptService;
import lzyc.pointsManagement.common.annotation.Log;
import lzyc.pointsManagement.common.core.controller.BaseController;
import lzyc.pointsManagement.common.core.domain.AjaxResult;
import lzyc.pointsManagement.common.core.domain.entity.SysDept;
import lzyc.pointsManagement.common.core.domain.entity.SysUser;
import lzyc.pointsManagement.common.core.page.TableDataInfo;
import lzyc.pointsManagement.common.core.text.Convert;
import lzyc.pointsManagement.common.enums.BusinessType;
import lzyc.pointsManagement.common.utils.DateUtils;
import lzyc.pointsManagement.common.utils.ShiroUtils;
import lzyc.pointsManagement.common.utils.StringUtils;
import lzyc.pointsManagement.framework.shiro.service.SysPasswordService;
import lzyc.pointsManagement.system.service.ISysDeptService;
import lzyc.pointsManagement.system.service.ISysRoleService;
import lzyc.pointsManagement.system.service.ISysUserService;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户信息
 *
 * @author ruoyi
 */
@Controller
@RequestMapping("/api/v1/users")
public class SysUserDeptController extends BaseController {

    @Autowired
    private ISysUserService userService;

    @Autowired
    private ISysRoleService roleService;

    @Autowired
    private ISysDeptService deptService;

    @Autowired
    private SysPasswordService passwordService;

    @Autowired
    private SysUserDeptService sysUserDeptService;

    /*
     *   普通用户角色ID
     */
    private String commonRoleID = RoleEnum.COMMON_USER.getValue();

    /**
     * 分页获取用户列表
     *
     * @param user
     * @return
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysUser user) {
        startPage();
        List<SysUserDept> list = sysUserDeptService.selectUserList(user);
        return getDataTable(list);
    }


    /**
     * 分页获取部门列表
     *
     * @param dept
     * @return
     */
    @PostMapping("/deptList")
    @ResponseBody
    public TableDataInfo deptList(SysDept dept) {
        List<SysDept> list = sysUserDeptService.deptList(dept);
        return getDataTable(list);
    }

    /**
     * 分页获取小组列表
     *
     * @param group
     * @return
     */
    @PostMapping("/groupList")
    @ResponseBody
    public TableDataInfo groupList(SysDept group) {
        List<SysDept> list = sysUserDeptService.groupList(group);
        return getDataTable(list);
    }
    /**
     * 新增保存用户
     */
    @Log(title = "用户管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@RequestBody SysUserDept userDept) {
        if (!sysUserDeptService.checkLoginNameUnique(userDept)) {
            return error("新增用户'" + userDept.getLoginName() + "'失败，登录账号已存在");
        }
        return toAjax(sysUserDeptService.addSave(userDept));
    }

    /**
     * 修改保存用户
     */
    @Log(title = "用户管理", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    @ResponseBody
    public AjaxResult editSave(@RequestBody SysUserDept userDept) {
        return toAjax(sysUserDeptService.editSave(userDept));
    }

    /**
     * 删除用户
     *
     * @param ids
     * @return
     */
    @Log(title = "用户管理", businessType = BusinessType.DELETE)
    @PostMapping("/delete")
    @ResponseBody
    public AjaxResult delete(String ids) {
        if (ArrayUtils.contains(Convert.toLongArray(ids), getUserId())) {
            return error("当前用户不能删除");
        }
        return toAjax(sysUserDeptService.deleteUserByIds(ids));
    }

    /**
     * 查询用户详细
     */
    @PostMapping("/view")
    @ResponseBody
    public AjaxResult view(String loginName) {
        return success(sysUserDeptService.selectUserByLoginName(loginName));
    }

    /**
     * 查询当前用户详细
     */
    @PostMapping("/info/get")
    @ResponseBody
    public AjaxResult getCurrentUserInfo() {
        return success(sysUserDeptService.selectUserById(getUserId()));
    }

    /**
     * 校验用户名
     */
    @PostMapping("/checkLoginNameUnique")
    @ResponseBody
    public boolean checkLoginNameUnique(@RequestBody SysUserDept user) {
        return sysUserDeptService.checkLoginNameUnique(user);
    }

}